<div class="tmd-doc">
<p></p>
<h1 class="tmd-header-1">
operator 2
</h1>
<p></p>
<div id="question" class="tmd-base">
<p></p>
<div class="tmd-usual">
What does the following program print?
</div>
<p></p>
<pre class="tmd-code line-numbers">
<code class="language-go">package main

func main() {
	count := 0
	for i := range [256]struct{}{} {
		if n := byte(i); n == -n {
			count++
		}
	}
	println(count)
}
</code></pre>
</div>
<p></p>
<div class="tmd-usual">
Choices:
</div>
<p></p>
<input type="radio" id="choiceA" name="choice" class="choice">
<input type="radio" id="choiceB" name="choice" class="choice">
<input type="radio" id="choiceC" name="choice" class="choice">
<input type="radio" id="choiceD" name="choice" class="choice">

<div id="choices">

<ul style="list-style-type:none;">
<li><label for="choiceA">0</label></li>
<li><label for="choiceB">1</label></li>
<li><label for="choiceC">2</label></li>
<li><label for="choiceD">3</label></li>
</ul>

</div><p></p>
<div id="answer" class="tmd-base">
<div class="tmd-usual">
Answer: 2
</div>
<p></p>
<div class="tmd-usual">
Run it on <a href="https://go.dev/play/p/qbtozNmX-Pv">Go play</a>.
</div>
<p></p>
<div class="tmd-usual">
Key points:
</div>
<ul class="tmd-list">
<li class="tmd-list-item">
<div class="tmd-usual">
In Go, for a <code class="tmd-code-span">byte</code> (a.k.a. <code class="tmd-code-span">uint8</code>) non-constant non-zero value <code class="tmd-code-span">x</code>, <code class="tmd-code-span">-x</code> overflows the range of type <code class="tmd-code-span">byte</code> and is wrarpped as <code class="tmd-code-span">(-x + 256) % 256</code>.
</div>
</li>
<li class="tmd-list-item">
<div class="tmd-usual">
For a variable <code class="tmd-code-span">x</code> of type <code class="tmd-code-span">byte</code>, <code class="tmd-code-span">x == -x</code> happens only when <code class="tmd-code-span">x</code> is <code class="tmd-code-span">0</code> or <code class="tmd-code-span">128</code>.
</div>
</li>
</ul>
<p></p>
</div>
<p></p>
</div>
